
package org.hive.utils 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	
	public class KeyframeAlbum
	{
		public static var keyframes:Array = new Array();
		public static var limit:Number = 9;
		
		public static function push( bmp:BitmapData, args:*=null ):Number
		{	
			if( keyframes.length == limit ) keyframes.shift();
			return keyframes.push( bmp );
		}
		
		public static function get length( ):Number
		{
			return keyframes.length;
		}
		
		public static function getKeyframe( index:Number=0 ):BitmapData
		{
			return keyframes[ index ];
		}
		
		public static function get lastKeyframe():BitmapData
		{
			return keyframes[ keyframes.length-1 ];
		}
		
		public static function forEach( f:Function ):void
		{
			keyframes.forEach( f );
		}
		
		public static function add( src:MovieClip, frame:Number, width:Number=-1, height:Number=-1, matrix:Matrix=null ):void
		{
			if( src.currentFrame != frame ) src.gotoAndStop( frame );
			
			if( src.mask ){
				var srcMask:DisplayObject = src.mask;
				src.mask = null;
			}
			
			if( matrix == null ) matrix = new Matrix( 1, 0, 0, 1, 0, 0 );
			if( width == -1 ) width = src.width;
			if( height == -1 ) height = src.height;
			
			var freezeFrame : BitmapData = new BitmapData( width, height, true, 0x00FFFFFF );
			freezeFrame.draw( src, matrix );
			if( srcMask ) src.mask = srcMask;
			push( freezeFrame );
		}
		
		
	}	
}
